/***************************************************************************
 *   Copyright (c) 2019 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com>     *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/

#include "pch.h"


#include "SketchGeometryExtension.h"
#include "Part/App/GeometryExtension.h"

using namespace Sketcher;


//---------- Geometry Extension
constexpr std::array<const char*, InternalType::NumInternalGeometryType> SketchGeometryExtension::internaltype2str;
constexpr std::array<const char*, GeometryMode::NumGeometryMode> SketchGeometryExtension::geometrymode2str;
TYPESYSTEM_SOURCE(Sketcher::SketchGeometryExtension,Part::GeometryPersistenceExtension)

// scoped within the class, multithread ready
std::atomic<long> SketchGeometryExtension::_GeometryID;

SketchGeometryExtension::SketchGeometryExtension(): Id(++_GeometryID),
                                                    InternalGeometryType(InternalType::None),
                                                    GeometryLayer(0) {}

SketchGeometryExtension::SketchGeometryExtension(long cid): Id(cid),
                                                            InternalGeometryType(InternalType::None),
                                                            GeometryLayer(0) {}

void SketchGeometryExtension::copyAttributes() const {}

std::unique_ptr<Part::GeometryExtension> SketchGeometryExtension::copy(void) const {return nullptr;}

bool SketchGeometryExtension::getInternalTypeFromName(std::string str, InternalType::InternalType& type) {
    auto pos = std::find_if(internaltype2str.begin(),
                            internaltype2str.end(),
                            [str](const char* val) {
                                return strcmp(val, str.c_str()) == 0;
                            }
    );

    if (pos != internaltype2str.end()) {
        int index = std::distance(internaltype2str.begin(), pos);

        type = static_cast<InternalType::InternalType>(index);
        return true;
    }

    return false;
}

bool SketchGeometryExtension::getGeometryModeFromName(std::string str, GeometryMode::GeometryMode& type) {
    auto pos = std::find_if(geometrymode2str.begin(),
                            geometrymode2str.end(),
                            [str](const char* val) {
                                return strcmp(val, str.c_str()) == 0;
                            }
    );

    if (pos != geometrymode2str.end()) {
        int index = std::distance(geometrymode2str.begin(), pos);

        type = static_cast<GeometryMode::GeometryMode>(index);
        return true;
    }

    return false;
}
